package com.dkd.manage.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.dkd.common.utils.DateUtils;
import com.dkd.manage.domain.dto.channelDto;
import com.dkd.manage.domain.dto.channelListDto;
import com.dkd.manage.domain.vo.ChannelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.ChannelMapper;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.service.IChannelService;

/**
 * 售货机货道Service业务层处理
 * 
 * @author gyw
 * @date 2025-03-07
 */
@Service
public class ChannelServiceImpl implements IChannelService 
{
    @Autowired
    private ChannelMapper channelMapper;

    /**
     * 查询售货机货道
     * 
     * @param id 售货机货道主键
     * @return 售货机货道
     */
    @Override
    public Channel selectChannelById(Long id)
    {
        return channelMapper.selectChannelById(id);
    }

    /**
     * 查询售货机货道列表
     * 
     * @param channel 售货机货道
     * @return 售货机货道
     */
    @Override
    public List<Channel> selectChannelList(Channel channel)
    {
        return channelMapper.selectChannelList(channel);
    }

    /**
     * 新增售货机货道
     * 
     * @param channel 售货机货道
     * @return 结果
     */
    @Override
    public int insertChannel(Channel channel)
    {
        channel.setCreateTime(DateUtils.getNowDate());
        return channelMapper.insertChannel(channel);
    }

    /**
     * 修改售货机货道
     * 
     * @param channel 售货机货道
     * @return 结果
     */
    @Override
    public int updateChannel(Channel channel)
    {
        channel.setUpdateTime(DateUtils.getNowDate());
        return channelMapper.updateChannel(channel);
    }

    /**
     * 批量删除售货机货道
     * 
     * @param ids 需要删除的售货机货道主键
     * @return 结果
     */
    @Override
    public int deleteChannelByIds(Long[] ids)
    {
        return channelMapper.deleteChannelByIds(ids);
    }

    /**
     * 删除售货机货道信息
     * 
     * @param id 售货机货道主键
     * @return 结果
     */
    @Override
    public int deleteChannelById(Long id)
    {
        return channelMapper.deleteChannelById(id);
    }

    /**
     * 批量插入售货机货道表
     * @param channelList
     */
    @Override

    public void insertChannelList(List<Channel> channelList) {
        channelMapper.insertChannelList(channelList);
    }

    /**
     * 根据设备编号查询关联的货道和商品列表数据
     * @param innerCode
     * @return
     */
    public List<ChannelVo> selectChannelVoByInnerCode(String innerCode) {
        List<ChannelVo> ChannelVolist = channelMapper.selectChannelVoByInnerCode(innerCode);
        return ChannelVolist;
    }

    /**
     * 关联售货机货道配置商品
     * @param channelListdto
     * @return
     */
    @Override
    public void ChannelConfigSky(channelListDto channelListdto) {

        List<channelDto> channelDtoList = channelListdto.getChannelList();
        List<Channel> ChannelList = new ArrayList();
        channelDtoList.forEach(channelDto -> {
            Channel channel = channelMapper.getChannelByInnerCodeAndChannelCode(channelDto.getInnerCode(),
                    channelDto.getChannelCode());
           if(channel!=null){
                channel.setSkuId(channelDto.getSkuId());
               // 货道更新时间
                channel.setUpdateTime(DateUtils.getNowDate());
            }
           ChannelList.add(channel);
       });
//        批量修改货道关联的商品
        channelMapper.updateChannelList(ChannelList);
    }


}
